package com.spider.know;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.spider.dao.HBaseDAOImp;
import com.spider.dao.HBaseDao;

public class KnowProcess {
	
	BufferedReader br;
	List<List<String>> knowList;
	List<String> knowContentList;
	List<List<String>> partList;
	List<String> partContentList;
	List<List<String>> chapterList;
	List<String> chapterContentList;
	
	public List<List<String>> processKnow(File file) {
		String line = "";
		knowList = new ArrayList<List<String>>();
		int index1 = 1;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
			while((line = br.readLine())!=null) {			
				knowContentList = new ArrayList<String>();
				for (String str : line.split(",")) {		
					String k = str.replaceAll("]", "").replaceAll(",", "").replaceAll("[?]", "");
					knowContentList.add(k);
				}
				index1++;
				knowList.add(knowContentList);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return knowList;
	}
	
	public List<List<String>> processPart(File file) {
		String line = "";
		partList = new ArrayList<List<String>>();
		int index1 = 1;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			while((line = br.readLine())!=null) {			
				partContentList = new ArrayList<String>();
				for (String str : line.split(",")) {		
					String k = str.replaceAll("]", "");
					partContentList.add(k);
				}
				partList.add(partContentList);
				index1++;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return partList;
	}
	
	public List<List<String>> processChapter(File file) {
		String line = "";
		chapterList = new ArrayList<List<String>>();
		int index1 = 1;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			while((line = br.readLine())!=null) {			
				chapterContentList = new ArrayList<String>();
				for (String str : line.split(",")) {		
					String k = str.replaceAll("]", "");
					chapterContentList.add(k);
				}
				chapterList.add(chapterContentList);
				index1++;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return chapterList;
	}
	
	public static void main(String[] args) {
		KnowProcess process = new KnowProcess();
		HBaseDao dao = new HBaseDAOImp();
		List<List<String>> knowList = process.processKnow(new File("./a"));
		List<List<String>> partList = process.processPart(new File("./b"));
		List<List<String>> chapterList = process.processChapter(new File("./c"));
		System.out.println(chapterList.size());
//		int[][] knowIndex1 = new int[][] {{4,3,4,4,1},{3,2,1,4,2,2},{3,4,6,3,4},{2,5,2,1,2,2,3}};
//		int[][] knowIndex2 = new int[][] {{4,5,1,2,1,2,1},{1,0,1,1,4,0},{3,2,2,2,1,1,2,1,1,3}};
//		int[][] knowIndex3 = new int[][] {{4,6,4,3,2,2},{3,3,4,4,0},{2,2,5,2,2,4,0},{1,5,4,6,1}};
//		int[][] knowIndex3 = new int[][] {{5,4,4,3},{2,1,6,4,1,0},{2,4,3,5,3},{2,0,1,2,0}};
//		int[][] knowIndex3 = new int[][] {{5,2,1,4,4},{4,6,2},{4,5,4},{6,2,4},{3,1,6,6},{1,2,3,1}};
//		int[][] knowIndex3 = new int[][] {{1,1,1,1,1,2},{0,1,1},{1,0,0,0,1},{2,2,1,1},{1,0,1}};
//		int[][] knowIndex3 = new int[][] {{1,2,2,5,1,2,4},{3,3,3,2,3},{1,2,1}};
//		int[][] knowIndex3 = new int[][] {{3,3,3,0,4},{1,3,4,5},{2,4,2,1},{1,3,2,3,1,3}};
//		int[][] knowIndex3 = new int[][] {{1,5,1,4,3},{4,1,1,4,1,1},{4,2,2,3,1,1,2,1},{2,1,1,6,4},{2,4,3,2}};
		int[][] knowIndex3 = new int[][] {{1,2,2,3,2},{2,4,4,1,2},{2,3,1,4},{4,1,0,3,5,6,3,3}};
		String[] quailifer = new String[] {"class","grade","chapter","part","know"};
		int RowKey = 586;
		int book = 11;
		int indexj = 0;
		int indexk = 0;
		
		for(int i = 0;i<knowIndex3.length;i++) {
			for(int j = 0;j< knowIndex3[i].length;j++) {
				for(int k = 0;k < knowIndex3[i][j];k ++) {
					String[] value = new String[] {"phy","ѡ��3-5",chapterList.get(book).get(i),partList.get(book).get(indexj),knowList.get(book).get(indexk)};
					dao.insert("Knowledge", (++RowKey)+"", "base", quailifer, value);
					indexk++;
					System.out.println("Row:"+RowKey);
				}
				indexj++;
			}
		}
	}
}
